package org.yiqixue.accounts.service;

import org.yiqixue.accounts.dto.TransactionDTO;
import org.yiqixue.accounts.entity.Accounts;
import org.yiqixue.accounts.entity.Transaction;
import org.yiqixue.accounts.repository.TransactionRepository;
import org.yiqixue.accounts.repository.AccountsRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class TransactionService {

    @Autowired
    private TransactionRepository transactionRepository;

    @Autowired
    private AccountsRepository accountsRepository;

    // 创建交易方法
    public TransactionDTO createTransaction(TransactionDTO transactionDTO) {
        // 获取账户信息
        Optional<Accounts> accountOptional = accountsRepository.findByPhoneNumber(transactionDTO.getPhoneNumber());

        if (!accountOptional.isPresent()) {
            throw new RuntimeException("Account not found for phone number: " + transactionDTO.getPhoneNumber());
        }

        Accounts account = accountOptional.get();

        // 创建交易实体
        Transaction transaction = new Transaction();
        transaction.setAccount(account);
        transaction.setAmount(transactionDTO.getAmount());
        transaction.setTransactionDate(transactionDTO.getTransactionDate()); // 使用LocalDateTime
        transaction.setDescription(transactionDTO.getDescription());
        transaction.setStatus(transactionDTO.getStatus());
        transaction.setType(transactionDTO.getType());

        Transaction savedTransaction = transactionRepository.save(transaction);

        // 返回DTO
        return new TransactionDTO(
                savedTransaction.getTransactionId(),
                savedTransaction.getAccount().getAccountNumber(),
                savedTransaction.getAmount(),
                savedTransaction.getTransactionDate(),
                savedTransaction.getDescription(),
                savedTransaction.getStatus(),
                savedTransaction.getType(),
                transactionDTO.getPhoneNumber()
        );
    }

    // 查询交易方法
    public List<TransactionDTO> getTransactionsByCustomerPhoneAndType(
            String phoneNumber,
            String type,
            LocalDateTime startDate,  // 改为LocalDateTime
            LocalDateTime endDate) {  // 改为LocalDateTime

        // 获取账户信息
        Optional<Accounts> accountOptional = accountsRepository.findByPhoneNumber(phoneNumber);

        if (!accountOptional.isPresent()) {
            throw new RuntimeException("Account not found for phone number: " + phoneNumber);
        }

        Accounts account = accountOptional.get();

        // 查询交易记录
        List<Transaction> transactions = transactionRepository
                .findByAccount_AccountNumberAndTypeAndTransactionDateBetween(
                        account.getAccountNumber(),
                        type,
                        startDate,
                        endDate);

        // 转换为DTO
        return transactions.stream()
                .map(transaction -> new TransactionDTO(
                        transaction.getTransactionId(),
                        transaction.getAccount().getAccountNumber(),
                        transaction.getAmount(),
                        transaction.getTransactionDate(),
                        transaction.getDescription(),
                        transaction.getStatus(),
                        transaction.getType(),
                        phoneNumber
                ))
                .collect(Collectors.toList());
    }
}